var current_loc;
var image_select_pos = {};
// var selected_tracks = new Array();
var locations = {};
var gene_info = {}
var ajaxData = {}
var ajaxRequests = new Array();
var pendingRequests = 0;

Ext.namespace('Ext.ux');
Ext.ux.sampleGrid = Ext.extend(Ext.grid.GridPanel, {
    selected_tracks: [],
    id: 'sampleGrid',
    samples: new Ext.data.Store(),
    initComponent: function() {

        Ext.apply(this, {
            store: new Ext.data.Store(),
            columns: config[current_genome].samples.columns,
            stripeRows: true,
            title: '',
            selModel: new Ext.grid.RowSelectionModel({
                singleSelect: false
            })
        })
        Ext.ux.sampleGrid.superclass.initComponent.apply(this, arguments);
        this.getSelectionModel().addListener({
            selectionchange: {
                fn: this.select_tracks,
                buffer: 50,
                scope: this
            }
        })

    },

    getSamples: function() {
        var params = {};
        Ext.applyIf(params, config[current_genome].samples.additional_params);

        Ext.Ajax.request({
            url: config[current_genome].samples.url,	
            method: 'GET',
            xdomain:true,
            params: params,
            scope: this,
            success: function(response, opts) {
                this.sampleData = Ext.decode(response.responseText)

                var fields = [];
                Ext.each(config[current_genome].samples.columns,
                function(x) {
                    fields.push({
                        mapping: x.dataIndex,
                        name: x.dataIndex
                    })
                })

                this.samples = new Ext.data.Store({
                    proxy: new Ext.data.MemoryProxy(this.sampleData),
                    reader: new Ext.data.JsonReader({}, fields)
                });
                this.samples.load();

                this.reconfigure(this.samples, 
                new Ext.grid.ColumnModel(config[current_genome].samples.columns));

                this.fields = fields;
                var row_ind = new Array();
                for (i in this.selected_tracks) {
                    row_ind.push(this.getStore().find('sample_id', this.selected_tracks[i]))
                }
                this.getSelectionModel().selectRows(row_ind);
            },
            failure: function(response, opts) {
                console.log('server-side failure with status code ' + response.status);
		            
            }
        });
    },

    addResults: function() {
        var columns = [];
        var fields = [];

        //console.log(columns)
        for (j in locations) {
            var loc = locations[j];
            
            columns.push({
                header: loc.genename,
                width: 75,
                sortable: true,
                dataIndex: loc.location_id
            })
            fields.push({
                mapping: loc.location_id,
                name: loc.location_id,
                type: 'float'
            });
            
            for (ii = 0; ii < this.sampleData.length; ii++) {
                var id = this.sampleData[ii].sample_id
                this.sampleData[ii][loc.location_id] = loc['cnts'][id];

            }
        }
        var store = new Ext.data.Store({
            proxy: new Ext.data.MemoryProxy(this.sampleData),
            reader: new Ext.data.JsonReader({},
            this.fields.concat(fields))
        });
        store.load();
        this.reconfigure(store, new Ext.grid.ColumnModel(config[current_genome].samples.columns.concat(columns)));
        var row_ind = new Array();
        for (i in this.selected_tracks) {
            row_ind.push(this.getStore().find('sample_id', this.selected_tracks[i]))
        }
        this.getSelectionModel().selectRows(row_ind);

    },
    select_tracks: function(selectionModel, rowIndex, selectedRecord) {
        var new_tracks = new Array();
        Ext.each(selectionModel.getSelections(),
        function(rec) {
            new_tracks.push(rec.data.sample_id);
        });
        // console.log('adf');
        this.selected_tracks = new_tracks;
        // console.log('adf');
        updateDisplay(false);

    },
    set_selected_tracks: function(tracks) {
        // console.log(tracks)
        this.selected_tracks = tracks;
        var row_ind = new Array();
        for (i in this.selected_tracks) {
            row_ind.push(this.getStore().find('sample_id', this.selected_tracks[i]))
        }
        this.getSelectionModel().selectRows(row_ind);
        updateDisplay(false);

    }


})


 var sampleGrid = new Ext.ux.sampleGrid();


